package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.WarehousePlaceMapper;
import com.indusfo.spc.pojo.WarehousePlace;
import com.indusfo.spc.service.WarehousePlaceService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class WarehousePlaceServiceImpl implements WarehousePlaceService {

    private  static  final Logger logger = LoggerFactory.getLogger(WarehousePlaceServiceImpl.class);

    @Autowired
    private WarehousePlaceMapper warehousePlaceMapper;

    @Override
    public JSONObject selectWarehousePlace(WarehousePlace warehousePlace) {
        List<WarehousePlace> warehousePlaceList = null;
        int count = 0;
        try {
            Integer pagesize = warehousePlace.getPagesize();
            Integer pageindex = warehousePlace.getPageindex();
            if(pagesize != null && pageindex != null) {
                warehousePlace.setIncept(pagesize*(pageindex - 1));
            }
            if(warehousePlace.getlWarehouse() == null) {
                throw new ParamsErrorException("请选择仓库");
            }
            warehousePlaceList = warehousePlaceMapper.selectWarehousePlace(warehousePlace);
            //查询分页总记录数
            count = warehousePlaceMapper.countWarehousePlace(warehousePlace); //返回查询到的总记录数
            if (warehousePlaceList.isEmpty()) {
                return JSONObject.oK("没有查询到相关数据", warehousePlaceList, count);
            }
            return JSONObject.oK("查询成功", warehousePlaceList, count);
        } catch (
                GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject insertWarehousePlace(WarehousePlace warehousePlace) {
        try {
            if(warehousePlace.getWarehousePlaceId()!= null) {
                throw new ParamsErrorException("新增时，不能填写id");
            }
            checkParam(warehousePlace);
            // 调用存储过程
            int row = warehousePlaceMapper.insertSelective(warehousePlace);
            if(row == 0) {
                throw new ModifyFailedException("数据新增失败");
            }
            return JSONObject.oK("新增成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject updateWarehousePlace(WarehousePlace warehousePlace) {
        try {
            if(warehousePlace.getWarehousePlaceId()== null) {
                throw new ParamsErrorException("请选择要更新的库位");
            }
            checkParam(warehousePlace);
            // 调用存储过程
            int row = warehousePlaceMapper.updateByPrimaryKeySelective(warehousePlace);
            if(row == 0) {
                throw new ModifyFailedException("数据更新失败！");
            }
            return JSONObject.oK("更新成功！");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject definesWarehousePlace(Long[] warehousePlaceIds, Integer dataState) {
        try {
            if(dataState == null) {
                throw new ParamsErrorException("数据状态不能为空");
            }else if(dataState != 1 && dataState != 2 && dataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if (warehousePlaceIds == null) {
                throw new ParamsErrorException("id不能为空");
            }
            // 执行存储过程
            int row = warehousePlaceMapper.definesWarehousePlace(warehousePlaceIds, dataState);
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数lDataState,返回相应信息
                switch (dataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                }
            } else {
                switch (dataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                }
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 参数校验
     */
    private void checkParam(WarehousePlace warehousePlace) {
        if (warehousePlace.getlWarehouse() == null) {
            throw new ParamsErrorException("请选择仓库");
        }
        if (StringUtils.isEmpty(warehousePlace.getWarehousePlaceCode())) {
            throw new ParamsErrorException("请填写库位编码");
        }
        if (warehousePlace.getWarehousePlaceCode().getBytes().length > 50) {
            throw new ParamsErrorException("库位编码超过规定,长度50");
        }
        if (StringUtils.isEmpty(warehousePlace.getWarehousePlaceName())) {
            throw new ParamsErrorException("请填写库位名称");
        }
        if (warehousePlace.getWarehousePlaceName().getBytes().length > 50) {
            throw new ParamsErrorException("库位名称超过规定,长度50");
        }
        // 编码不能重复
        Integer depCounts = warehousePlaceMapper.queryWarehousePlace(warehousePlace);
        if (depCounts !=0) {
            throw new ParamsErrorException("该仓库编码已存在");
        }
    }

}
